package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;


import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.interfaces.services.IVisitorService;
import es.ats.codington.festival.utils.FERSDataConnection;
/*
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
*/

public class VisitorService implements IVisitorService{

	Connection connection=null;
	
/**
 * 
 * @param u
 * @return
 * @throws SQLException
 * @throws ClassNotFoundException
 */
	
	
public boolean registerNewVisitor(Visitor u) throws SQLException, ClassNotFoundException{
		
		boolean bool = false;
		

		try {
			
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);
			bool = visitordaoimpl.registerNewVisitorDAO(u);
		} catch(Exception e) {
			System.out.println("registerNewVisitor: "+ e.getMessage());
			return false;
			}
		
			connection.close();

		return bool;
	}
	
	
	
	
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	
	public boolean updateVisitor(Visitor u) throws SQLException, ClassNotFoundException{

		
		boolean bool=false;
		
		try {
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			bool=visitordaoimpl.updateVisitorDAO(u);
						
		}catch (Exception ee) {
			System.out.println("updateVisitor: "+ ee.getMessage());
			return false;
		} 

			connection.close();
		
			return bool;

	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public int getIdVisitor(Visitor u) throws SQLException{
		
		int idVisitor = 0;
		
		try {
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			idVisitor=visitordaoimpl.getIdVisitorDAO(u);
						
		}catch (Exception ee) {
			System.out.println("login: "+ ee.getMessage());
		} 

			connection.close();


		return idVisitor;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public boolean login(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			
						
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			bool=visitordaoimpl.loginDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 

			connection.close();


		return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public boolean isAdmin(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			bool=visitordaoimpl.isAdminDAO(u);
						
		}catch (Exception ee) {
			ee.getMessage();
			return false;
		} 

			connection.close();


		return bool;
	}
	
	/**
	 * 
	 * @param u
	 * @return 
	 * @throws SQLException
	 */
	
	public boolean unregisterVisitor(Visitor u) throws SQLException{
		
		boolean bool=false;
		
		try {
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			bool=visitordaoimpl.unregisterVisitorDAO(u);
						
		}catch (Exception ee) {
			System.out.println("unregistervisitorService: "+ ee.getMessage());
			return false;
		} 

			connection.close();


		return bool;
	}
	
public Visitor getVisitor(Visitor u) throws SQLException{
		
		
		try {
			connection = FERSDataConnection.getConnection();
			VisitorDAOImpl visitordaoimpl=new VisitorDAOImpl(connection);  
			u=visitordaoimpl.getVisitorDAO(u);
						
		}catch (Exception ee) {
			System.out.println("getvisitorService: "+ ee.getMessage());
		} 

			connection.close();


		return u;
	}
	
	
	
	
}
